package com.etc.controller;

import com.etc.entity.CommentResult;
import com.etc.entity.Household;
import com.etc.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class HouseholdController {
    @Autowired
    private HouseholdService householdService;

    @Autowired
    private OwnerService ownerService;

    @Autowired
    private RoomService roomService;

    @Autowired
    private BuildingService buildingService;
    @Autowired
    private ParkService parkService;
    @RequestMapping("household/findAllHousehold")
    public CommentResult findAllHousehold(@RequestParam(value = "start", required = false) Integer start,
                                          @RequestParam(value = "size", required = false) Integer size){

        List<Household> list = householdService.findAllHousehold();
        List<Map<String,Object>> result = new ArrayList<>();
        for (int i = 0;i<list.size();i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", list.get(i).getId());
            map.put("name", list.get(i).getName());
            map.put("sex", list.get(i).getSex());
            map.put("age",list.get(i).getAge());
            map.put("park_name", parkService.findById(roomService.findById(list.get(i).getRoom_id()).getPark_id()).getPark_name());
            map.put("building_name", buildingService.findById(roomService.findById(list.get(i).getRoom_id()).getBuilding_id()).getName());
            if (ownerService.findByRoom(list.get(i).getRoom_id())==null){
                map.put("owner_name", null);
            }else {
                map.put("owner_name", ownerService.findByRoom(list.get(i).getRoom_id()).getName());
            }
            map.put("building_id",roomService.findById(list.get(i).getRoom_id()).getBuilding_id());
            map.put("park_id",roomService.findById(list.get(i).getRoom_id()).getPark_id());
            map.put("room_num", roomService.findById(list.get(i).getRoom_id()).getRoom_num());
            map.put("room_id", roomService.findById(list.get(i).getRoom_id()).getId());
            map.put("id_number", list.get(i).getId_number());
            map.put("phone", list.get(i).getPhone());
            map.put("check_in", list.get(i).getCheck_in());
            map.put("check_out", list.get(i).getCheck_out());
            result.add(map);
        }
        return new CommentResult(200,"success",result);
    }

    @RequestMapping("household/findByAnyCondition")
    public CommentResult findByAnyCondition(@RequestBody Map<String, Object> map,@RequestParam(value = "start", required = false) Integer start,
                                            @RequestParam(value = "size", required = false) Integer size){


        List<Household> list = householdService.findByAnyCondition(map);
        List<Map<String,Object>> result = new ArrayList<>();
        for (int i = 0;i<list.size();i++) {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("id", list.get(i).getId());
            map1.put("name", list.get(i).getName());
            map1.put("sex", list.get(i).getSex());
            map1.put("age",list.get(i).getAge());
            map1.put("park_name", parkService.findById(roomService.findById(list.get(i).getRoom_id()).getPark_id()).getPark_name());
            map1.put("building_name", buildingService.findById(roomService.findById(list.get(i).getRoom_id()).getBuilding_id()).getName());
            if (ownerService.findByRoom(list.get(i).getRoom_id())==null){
                map1.put("owner_name", null);
            }else {
                map1.put("owner_name", ownerService.findByRoom(list.get(i).getRoom_id()).getName());
            }
            map1.put("building_id",roomService.findById(list.get(i).getRoom_id()).getBuilding_id());
            map1.put("park_id",roomService.findById(list.get(i).getRoom_id()).getPark_id());
            map1.put("room_num", roomService.findById(list.get(i).getRoom_id()).getRoom_num());
            map1.put("room_id", roomService.findById(list.get(i).getRoom_id()).getId());
            map1.put("id_number", list.get(i).getId_number());
            map1.put("phone", list.get(i).getPhone());
            map1.put("check_in", list.get(i).getCheck_in());
            map1.put("check_out", list.get(i).getCheck_out());
            result.add(map1);
        }
        return new CommentResult(200,"success",result);

    }

    @RequestMapping("houselold/findById/{id}")
    public CommentResult findById(@PathVariable("id")Integer id)
    {
        Household result = householdService.findById(id);
        List<Map<String,Object>> result1 = new ArrayList<>();

        Map<String, Object> map = new HashMap<>();
        map.put("id", result.getId());
        map.put("name", result.getName());
        map.put("sex", result.getSex());
        map.put("age",result.getAge());
        map.put("building_id",roomService.findById(result.getRoom_id()).getBuilding_id());
        map.put("park_id",roomService.findById(result.getRoom_id()).getPark_id());
        map.put("park_name", parkService.findById(roomService.findById(result.getRoom_id()).getPark_id()).getPark_name());
        map.put("building_name", buildingService.findById(roomService.findById(result.getRoom_id()).getBuilding_id()).getName());
        if (ownerService.findByRoom(result.getRoom_id())==null){
            map.put("owner_name", null);
        }else {
            map.put("owner_name", ownerService.findByRoom(result.getRoom_id()).getName());
        }
        map.put("room_id", roomService.findById(result.getRoom_id()).getId());
        map.put("room_num", roomService.findById(result.getRoom_id()).getRoom_num());
        map.put("id_number", result.getId_number());
        map.put("phone", result.getPhone());
        map.put("check_in", result.getCheck_in());
        map.put("check_out", result.getCheck_out());
        result1.add(map);

        return new CommentResult(200,"success",result1);

    }

    @RequestMapping("house/findByRoom/{id}")
    public CommentResult findByRoom(@PathVariable("id")Integer id,@RequestParam(value = "start", required = false) Integer start,
                                    @RequestParam(value = "size", required = false) Integer size){

        List<Household> list = householdService.findByRoom(id);
        List<Map<String,Object>> result = new ArrayList<>();
        for (int i = 0;i<list.size();i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", list.get(i).getId());
            map.put("name", list.get(i).getName());
            map.put("sex", list.get(i).getSex());
            map.put("age",list.get(i).getAge());
            map.put("park_name", parkService.findById(roomService.findById(list.get(i).getRoom_id()).getPark_id()).getPark_name());
            map.put("building_name", buildingService.findById(roomService.findById(list.get(i).getRoom_id()).getBuilding_id()).getName());
            if (ownerService.findByRoom(list.get(i).getRoom_id())==null){
                map.put("owner_name", null);
            }else {
                map.put("owner_name", ownerService.findByRoom(list.get(i).getRoom_id()).getName());
            }
            map.put("building_id",roomService.findById(list.get(i).getRoom_id()).getBuilding_id());
            map.put("park_id",roomService.findById(list.get(i).getRoom_id()).getPark_id());
            map.put("room_num", roomService.findById(list.get(i).getRoom_id()).getRoom_num());
            map.put("room_id", roomService.findById(list.get(i).getRoom_id()).getId());
            map.put("id_number", list.get(i).getId_number());
            map.put("phone", list.get(i).getPhone());
            map.put("check_in", list.get(i).getCheck_in());
            map.put("check_out", list.get(i).getCheck_out());
            result.add(map);
        }
        return new CommentResult(200,"success",result);

    }

    @RequestMapping("household/findInByTime")
    public CommentResult findInByTime(String fromtime,String totime,@RequestParam(value = "start", required = false) Integer start,
                                      @RequestParam(value = "size", required = false) Integer size){
        Date fromdate = null;
        Date todate = null;
        try {
            fromdate = new SimpleDateFormat("yyyy-MM-dd").parse(fromtime);
            todate = new SimpleDateFormat("yyyy-MM-dd").parse(totime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        List<Household> list = householdService.findInByTime(fromdate, todate);
        List<Map<String,Object>> result = new ArrayList<>();
        for (int i = 0;i<list.size();i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", list.get(i).getId());
            map.put("name", list.get(i).getName());
            map.put("building_id",roomService.findById(list.get(i).getRoom_id()).getBuilding_id());
            map.put("park_id",roomService.findById(list.get(i).getRoom_id()).getPark_id());
            map.put("age",list.get(i).getAge());
            map.put("sex", list.get(i).getSex());
            map.put("park_name", parkService.findById(roomService.findById(list.get(i).getRoom_id()).getPark_id()).getPark_name());
            map.put("building_name", buildingService.findById(roomService.findById(list.get(i).getRoom_id()).getBuilding_id()).getName());
            if (ownerService.findByRoom(list.get(i).getRoom_id())==null){
                map.put("owner_name", null);
            }else {
                map.put("owner_name", ownerService.findByRoom(list.get(i).getRoom_id()).getName());
            }
            map.put("room_num", roomService.findById(list.get(i).getRoom_id()).getRoom_num());
            map.put("room_id", roomService.findById(list.get(i).getRoom_id()).getId());
            map.put("id_number", list.get(i).getId_number());
            map.put("phone", list.get(i).getPhone());
            map.put("check_in", list.get(i).getCheck_in());
            map.put("check_out", list.get(i).getCheck_out());
            result.add(map);
        }
        return new CommentResult(200,"success",result);
    }

    @RequestMapping("household/findOutByTime")
    public CommentResult findOutByTime(String fromtime,String totime,@RequestParam(value = "start", required = false) Integer start,
                                       @RequestParam(value = "size", required = false) Integer size){
        Date fromdate = null;
        Date todate = null;
        try {
            fromdate = new SimpleDateFormat("yyyy-MM-dd").parse(fromtime);
            todate = new SimpleDateFormat("yyyy-MM-dd").parse(totime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        List<Household> list = householdService.findOutByTime(fromdate, todate);
        List<Map<String,Object>> result = new ArrayList<>();
        for (int i = 0;i<list.size();i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", list.get(i).getId());
            map.put("name", list.get(i).getName());
            map.put("age",list.get(i).getAge());
            map.put("sex", list.get(i).getSex());
            map.put("building_id",roomService.findById(list.get(i).getRoom_id()).getBuilding_id());
            map.put("park_id",roomService.findById(list.get(i).getRoom_id()).getPark_id());
            map.put("park_name", parkService.findById(roomService.findById(list.get(i).getRoom_id()).getPark_id()).getPark_name());
            map.put("building_name", buildingService.findById(roomService.findById(list.get(i).getRoom_id()).getBuilding_id()).getName());
            if (ownerService.findByRoom(list.get(i).getRoom_id())==null){
                map.put("owner_name", null);
            }else {
                map.put("owner_name", ownerService.findByRoom(list.get(i).getRoom_id()).getName());
            }
            map.put("room_num", roomService.findById(list.get(i).getRoom_id()).getRoom_num());
            map.put("room_id", roomService.findById(list.get(i).getRoom_id()).getId());
            map.put("id_number", list.get(i).getId_number());
            map.put("phone", list.get(i).getPhone());
            map.put("check_in", list.get(i).getCheck_in());
            map.put("check_out", list.get(i).getCheck_out());
            result.add(map);
        }
        return new CommentResult(200,"success",result);
    }

    @RequestMapping("household/findByPark/{id}")
    public CommentResult findByPark(@PathVariable("id")Integer id,@RequestParam(value = "start", required = false) Integer start,
                                    @RequestParam(value = "size", required = false) Integer size)
    {


        List<Household> list = householdService.findByPark(id);
        List<Map<String,Object>> result = new ArrayList<>();
        for (int i = 0;i<list.size();i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", list.get(i).getId());
            map.put("name", list.get(i).getName());
            map.put("age",list.get(i).getAge());
            map.put("sex", list.get(i).getSex());
            map.put("building_id",roomService.findById(list.get(i).getRoom_id()).getBuilding_id());
            map.put("park_id",roomService.findById(list.get(i).getRoom_id()).getPark_id());
            map.put("park_name", parkService.findById(roomService.findById(list.get(i).getRoom_id()).getPark_id()).getPark_name());
            map.put("building_name", buildingService.findById(roomService.findById(list.get(i).getRoom_id()).getBuilding_id()).getName());
            if (ownerService.findByRoom(list.get(i).getRoom_id())==null){
                map.put("owner_name", null);
            }else {
                map.put("owner_name", ownerService.findByRoom(list.get(i).getRoom_id()).getName());
            }
            map.put("room_num", roomService.findById(list.get(i).getRoom_id()).getRoom_num());
            map.put("room_id", roomService.findById(list.get(i).getRoom_id()).getId());
            map.put("id_number", list.get(i).getId_number());
            map.put("phone", list.get(i).getPhone());
            map.put("check_in", list.get(i).getCheck_in());
            map.put("check_out", list.get(i).getCheck_out());
            result.add(map);
        }
        return new CommentResult(200,"success",result);
    }

    @RequestMapping("household/findByBuilding/{id}")
    public CommentResult findByBuilding(@PathVariable("id")Integer id,@RequestParam(value = "start", required = false) Integer start,
                                        @RequestParam(value = "size", required = false) Integer size)
    {

        List<Household> list = householdService.findByBuilding(id);
        List<Map<String,Object>> result = new ArrayList<>();
        for (int i = 0;i<list.size();i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", list.get(i).getId());
            map.put("name", list.get(i).getName());
            map.put("age",list.get(i).getAge());
            map.put("sex", list.get(i).getSex());
            map.put("building_id",roomService.findById(list.get(i).getRoom_id()).getBuilding_id());
            map.put("park_id",roomService.findById(list.get(i).getRoom_id()).getPark_id());
            map.put("park_name", parkService.findById(roomService.findById(list.get(i).getRoom_id()).getPark_id()).getPark_name());
            map.put("building_name", buildingService.findById(roomService.findById(list.get(i).getRoom_id()).getBuilding_id()).getName());
            if (ownerService.findByRoom(list.get(i).getRoom_id())==null){
                map.put("owner_name", null);
            }else {
                map.put("owner_name", ownerService.findByRoom(list.get(i).getRoom_id()).getName());
            }
            map.put("room_num", roomService.findById(list.get(i).getRoom_id()).getRoom_num());
            map.put("room_id", roomService.findById(list.get(i).getRoom_id()).getId());
            map.put("id_number", list.get(i).getId_number());
            map.put("phone", list.get(i).getPhone());
            map.put("check_in", list.get(i).getCheck_in());
            map.put("check_out", list.get(i).getCheck_out());
            result.add(map);
        }
        return new CommentResult(200,"success",result);
    }


    @RequestMapping("household/findByOwner/{id}")
    public CommentResult findByOwner(@PathVariable("id")Integer id,@RequestParam(value = "start", required = false) Integer start,
                                     @RequestParam(value = "size", required = false) Integer size) {

        List<Household> list = householdService.findByOwner(id);
        List<Map<String,Object>> result = new ArrayList<>();
        for (int i = 0;i<list.size();i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", list.get(i).getId());
            map.put("name", list.get(i).getName());
            map.put("age",list.get(i).getAge());
            map.put("sex", list.get(i).getSex());
            map.put("building_id",roomService.findById(list.get(i).getRoom_id()).getBuilding_id());
            map.put("park_id",roomService.findById(list.get(i).getRoom_id()).getPark_id());
            map.put("park_name", parkService.findById(roomService.findById(list.get(i).getRoom_id()).getPark_id()).getPark_name());
            map.put("building_name", buildingService.findById(roomService.findById(list.get(i).getRoom_id()).getBuilding_id()).getName());
            if (ownerService.findByRoom(list.get(i).getRoom_id())==null){
                map.put("owner_name", null);
            }else {
                map.put("owner_name", ownerService.findByRoom(list.get(i).getRoom_id()).getName());
            }

            map.put("room_num", roomService.findById(list.get(i).getRoom_id()).getRoom_num());
            map.put("room_id", roomService.findById(list.get(i).getRoom_id()).getId());
            map.put("id_number", list.get(i).getId_number());
            map.put("phone", list.get(i).getPhone());
            map.put("check_in", list.get(i).getCheck_in());
            map.put("check_out", list.get(i).getCheck_out());
            result.add(map);
        }
        return new CommentResult(200,"success",result);

    }
}
